﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator_UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.Items.Clear();    //给下拉框赋值
            comboBox1.Items.Add("+");
            comboBox1.Items.Add("-");
            comboBox1.Items.Add("*");
            comboBox1.Items.Add("/");
        }

        private void calculate_Click(object sender, EventArgs e)
        {
            try
            {
                double num1 = Double.Parse(textBoxNum1.Text);    //获取两个操作数
                double num2 = Double.Parse(textBoxNum2.Text);

                switch (comboBox1.Text)                          //获取操作符，并运算
                {
                    case "+": label_result.Text = ":" + (num1 + num2); break;
                    case "-": label_result.Text = ":" + (num1 - num2);break;
                    case "*": label_result.Text = ":" + (num1 * num2);break;
                    case "/":
                        if (num2 == 0)                           //对除数进行检查
                            label_result.Text = "The num2 can't be 0!";
                        else
                            label_result.Text = ":" + (num1 / num2);
                        break;
                    default:
                        label_result.Text = "Please choose a correct operator!";
                        break;
                }
            }                                                     //异常处理
            catch (FormatException) { label_result.Text = "Format Error!!!";return; }
            catch (OverflowException) { label_result.Text = "Overflow!!!";return; }
        }
    }
}
